package org.summerframework.core.captcha;

import org.summerframework.core.util.SpringContextUtil;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.base.Captcha;
import org.springframework.boot.context.properties.bind.BindResult;
import org.springframework.boot.context.properties.bind.Binder;

import java.awt.*;

/**
 * @author 石超
 * @version v1.0.0
 */
public class NumberCaptcha extends SpecCaptcha {
    private boolean test = false;

    public NumberCaptcha() {
    }

    public NumberCaptcha(int width, int height) {
        super(width, height);
        init();
    }

    public NumberCaptcha(int width, int height, int len) {
        super(width, height, len);
        init();
    }

    public NumberCaptcha(int width, int height, int len, Font font) {
        super(width, height, len, font);
        init();
    }

    public NumberCaptcha(int width, int height, int len, Font font, Boolean test) {
        super(width, height, len, font);

        if (test != null) {
            this.test = test;
        }

        init();
    }

    private void init() {
        Binder binder = Binder.get(SpringContextUtil.getApplicationContext().getEnvironment());
        final BindResult<Boolean> bind = binder.bind("application.captcha", Boolean.class);
        setTest(bind.orElse(false));
        setCharType(Captcha.TYPE_ONLY_NUMBER);
    }

    public void setTest(boolean test) {
        this.test = test;

        if (test) {
            this.chars = "111111";
        }
    }
}
